Skip to content

fix: Include TenantId and UserId in GlobalExceptionHandler Logs#1221

Open
cesarcastrocuba wants to merge 1 commit into
fullstackhero:mainfrom
cesarcastrocuba:fix/web-tenantid-error-logs
Open

fix: Include TenantId and UserId in GlobalExceptionHandler Logs#1221
cesarcastrocuba wants to merge 1 commit into
fullstackhero:mainfrom
cesarcastrocuba:fix/web-tenantid-error-logs

Conversation

@cesarcastrocuba
Copy link
Copy Markdown
Contributor

[fix]: Include TenantId and UserId in GlobalExceptionHandler Logs

Description

This PR enhances the GlobalExceptionHandler to improve observability in multi-tenant environments. It ensures that every exception logged includes the relevant tenant_id and user_id context.

Key Changes:

  • DI Scope Conflict Resolution: Refactored GlobalExceptionHandler to resolve IMultiTenantContextAccessor and ICurrentUser dynamically from HttpContext.RequestServices. This prevents System.InvalidOperationException when a Singleton handler attempts to consume Scoped services.
  • Serilog LogContext Enrichment: Automatically pushes tenant_id and user_id to the Serilog LogContext when an exception occurs.
  • Enhanced Log Messages: Updated the error log template to explicitly show the Tenant and User involved (e.g., Exception at /api/endpoint (Tenant: root, User: Anonymous) - Detail...).

Related Issues

Verification

  • Unit Testing: Added GlobalExceptionHandlerTests in Generic.Tests using NSubstitute to verify context extraction and log enrichment.
  • Build: Success with zero errors.
  • Manual Check: Verified that the application starts correctly and the builder.Build() AggregateException is resolved.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

…[I-17]

- Pushes tenant_id and user_id to Serilog LogContext for all exceptions- Resolves scoped services (ICurrentUser, IMultiTenantContextAccessor) from RequestServices to avoid DI scope conflicts- Added unit tests in Generic.Tests to verify context extraction
@iammukeshm iammukeshm deleted the branch fullstackhero:main May 27, 2026 06:59
@iammukeshm iammukeshm closed this May 27, 2026
@iammukeshm iammukeshm reopened this May 27, 2026
@iammukeshm iammukeshm changed the base branch from develop to main May 27, 2026 07:01
@iammukeshm
Copy link
Copy Markdown
Member

Thanks for the substantial work across these PRs 🙏

FullStackHero has moved to a major new baseline — v10 (.NET 10) — which includes a significant restructure (for example src/api/framework/** is now src/BuildingBlocks/**) and a new CI pipeline. This PR now targets main but no longer applies cleanly against it.

Could you please:

  1. Rebase your branch onto the current main and resolve conflicts,
  2. re-review your changes against the v10 layout/APIs (some files have moved or changed), and
  3. push the update so the PR is green against the new Backend CI / Frontend CI checks.

Once it's rebased and passing we'll be happy to review. Really appreciate the contributions to FullStackHero!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants